var bt=baidu.template;//模板
file_id=5;
var multiple=1;//多选
file_group_id=1;
$.messager.progress({interval:500});
$('#saveButton').bind('click', save);//新增行绑定事件
//列表展示
$('#openuploadButton').bind('click', function (){
	multiple=0;
	$('#dd').dialog('open');
});
//var param1={"file_id":file_id,"multiple":0};
$('#dd').dialog({
    title: '选择图片',
    width: 900,
    height: 500,
    closed: true,
    closable:true,
    cache: false,
    href: '/view/sys/pub_file/image_manager.html',
    modal: true
});
/*var d2;
//var param2={"file_id":file_id,"multiple":1};
$('#openuploadButton2').bind('click', function (){
	if(!d2){
		d2= $("<div></div>");
	    d2.appendTo('body');
	    d2.dialog({
	        title: '上传证书',
	        width: 900,
	        height: 500,
	        closed: true,
	        closable:true,
	        cache: false,
	        href: '/view/sys/pub_file/image_manager.html',
	        modal: true
	    });
	}
	d2.dialog('open');
});*/
var myOption={
		file_id:6,
		uploadfiles:"uploadfiles",//开始上传按钮id
		multi_selection:false,//一次选择多个文件
		filters : {
            mime_types: [ 
                {title : "cert files", extensions : "p12"}  
            ]
        },
		FileUploadedCallBack: function(up, file, data){//单个文件上传后回调
			if(file.status==plupload.DONE){
				var item_id = data.item_id;
				$("input[name=apiclient_cert]").val(item_id);
				$("#cert_name").textbox('setValue',file.name);
			}
		},
		FilesAddedCallBack:function(up, files){//添加文件后回调
			var f=files[0];
			$("#certFileName").text(f.name);
			$("#cert").show();
			//$.messager.alert("提示", JSON.stringify(files));
		},
		UploadCompleteCallBack: function(up, files) {//全部上传完成回调
			
		}
};
var uploader1=new uniontrip.upload(myOption);
//删除证书
function deleteCert(em){
	$("input[name=apiclient_cert]").val("");
	$("#cert_name").textbox('setValue',"");
	//$(em).parent().remove();
	$("#cert").hide();
	uploader1.uploader.removeFile(uploader1.uploader.files[0].id);
}
/**
 * 商品详情文件上传完成
 * @param item_id
 * @param access_url
 * @returns
 */
//商品列表
function uploaded(array){
	if(multiple==0){//列表展示
		if(array.length>0){
			var html=bt('t_2',{data:array});
			$("#picture").html(html);
			//$("input[name=picture]").val(array[0].access_url);
			/*$.each(array,function(i,v){
				if(i==0){
					$('#picture').val(v.access_url);
				}
				file_item_idsArray.push(v.item_id);
			});
			$('#file_item_ids').val(file_item_idsArray.join());*/
		}
	}
	$('#dd').dialog('close');
}
//删除列表图片
function deletePicture(em){
	//$("input[name=picture]").val("");
	$(em).parent().remove();
}
//document ready
$(function(){
	//编辑填充表单
	$._post("/authenticated/ajax/map/select_get_my_wx_account",JSON.stringify({}),function(result){
		if(result.success){
			if(result.data){
				$("#url").text("http://www.lvzhi.com/weixin/"+result.data.id+"/"+result.data.uuid);
				$('#form0').form('load',result.data);
				if(result.data.account_qrcode){
					var html=bt('t_2',{data:[{"access_url":result.data.account_qrcode}]});
					$("#picture").html(html);
				}
				$.messager.progress("close");
			}else{
				$.messager.alert("提示","登录账号未绑定公共号信息！");
			}
		}
	});
});
function save(){
	var formObject=$("#form0").serializeObject();
	//赋值图片
	var picture=$("#picture").find("img").attr("src");
	if(picture){
		formObject.account_qrcode=picture;
	}
	$._post("/authenticated/ajax/update/update_my_wx_account",JSON.stringify(formObject),function (result){
		if(result.success){
			$.messager.alert("提示","保存成功！");
			$._post("/authenticated/upload/saveSingle",JSON.stringify({
				file_id:6,
				tartget_id:formObject.id,
				file_item_id:formObject.apiclient_cert
			}),function (result){
				
			});
			//删除旧的文件信息
		}else{
			$.messager.alert("提示", result.message);
		}
	});
}